package org.gcb.core.ui.dnd;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import java.util.List;
import javax.swing.JPanel;
import org.gcb.core.model.Armor;
import org.gcb.core.model.EArmorSlot;
import org.gcb.core.model.Item;
import org.gcb.core.model.Toon;
import org.gcb.core.ui.Utilities;
import org.gcb.core.windows.BackpackTopComponent;
import org.gcb.core.windows.DefensesTopComponent;
import org.gcb.core.windows.EquipmentTopComponent;
import org.gcb.core.windows.SkillsTopComponent;
import org.openide.util.Exceptions;
import org.openide.util.LookupEvent;
import org.openide.windows.WindowManager;

/**
 *
 * @author Jason
 */
public class ArmorDropTargetListener implements DropTargetListener{
    private Toon toon;
    private EArmorSlot slot;
    private JPanel panel;

    public ArmorDropTargetListener(JPanel panel, Toon toon, EArmorSlot slot) {
        this.toon = toon;
        this.slot = slot;
        this.panel = panel;
    }

    @Override
    public void dragEnter(DropTargetDragEvent dtde) {
	Transferable t = dtde.getTransferable();
	Object obj = null;
	for (DataFlavor flavor : t.getTransferDataFlavors()) {
	    try {
		obj = t.getTransferData(flavor);
	    } catch (UnsupportedFlavorException ex) {
		Exceptions.printStackTrace(ex);
	    } catch (IOException ex) {
		Exceptions.printStackTrace(ex);
	    }
	}
    }

    @Override
    public void dragOver(DropTargetDragEvent dtde) {}

    @Override
    public void dropActionChanged(DropTargetDragEvent dtde) {}

    @Override
    public void dragExit(DropTargetEvent dte) {}

    @Override
    public void drop(DropTargetDropEvent dtde) {
	List<DataFlavor> flavors = dtde.getCurrentDataFlavorsAsList();
	boolean answer = flavors.contains(Item.ITEM_DATA_FLAVOR);
        if (dtde.isDataFlavorSupported(Item.ITEM_DATA_FLAVOR) && toon.getEquipment().getWorn().isEmpty(slot)) {
            try {
                Object obj = dtde.getTransferable().getTransferData(Item.ITEM_DATA_FLAVOR);
                if (obj instanceof Armor) {
                    Armor armor = (Armor) obj;
                    if (armor.getSlot()==slot) {
                        toon.getEquipment().getWorn().put(slot, armor);//could easily change this to a swap instead
                        toon.getEquipment().getCarried().remove(armor);
                        panel.setToolTipText(armor.getDescription());
                        LookupEvent event = new LookupEvent(WindowManager.getDefault().findTopComponent("ToonListingTopComponent").getLookup().lookupResult(Toon.class));
                        Utilities.updateUI(event, BackpackTopComponent.findInstance(), DefensesTopComponent.findInstance(), EquipmentTopComponent.findInstance(), SkillsTopComponent.findInstance());
                    } else {
                        dtde.rejectDrop();
                        dtde.dropComplete(true);
                    }
                }
            } catch (UnsupportedFlavorException ex) {
                dtde.rejectDrop();
                dtde.dropComplete(true);
                Exceptions.printStackTrace(ex);
            } catch (IOException ex) {
                dtde.rejectDrop();
                dtde.dropComplete(false);
                Exceptions.printStackTrace(ex);
            }
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }
    }

}
